//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using System.ComponentModel;
using System.Text;

namespace CommunityServer.Controls 
{
	// *********************************************************************
	//  WhoIsOnline
	//
	/// <summary>
	/// This server control is used to display a list of users currently online.
	/// </summary>
	// ***********************************************************************/
	public class WhoIsOnlineScroller : ContentScroller
	{
		CSContext csContext = CSContext.Current;

		#region Event Handlers

		protected override void OnLoad(EventArgs e) 
		{
//			// If the user is already authenticated we have no work to do
//			//
//			if (!csContext.SiteSettings.EnableWhoIsOnline)
//			{
//				this.Visible = false;
//				return;
//			}

			DataBind();

			base.OnLoad(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td><div class=\"");
			writer.Write(this.LeftNavigationCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".MovePreviousPage();\">&nbsp;</div></td><td width=\"100%\">");

			base.Render(writer);
	
			writer.Write("</td><td><div class=\"");
			writer.Write(this.RightNavigationCssClass);
			writer.Write("\" onclick=\"");
			writer.Write(this.ClientID);
			writer.Write(".MoveNextPage();\">&nbsp;</div></td></tr></table>");
		}

		#endregion

		#region Properties

		public string LeftNavigationCssClass
		{
			get
			{
				string state = (string) ViewState["LeftNavigationCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["LeftNavigationCssClass"] = value;
			}
		}

		public string RightNavigationCssClass
		{
			get
			{
				string state = (string) ViewState["RightNavigationCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["RightNavigationCssClass"] = value;
			}
		}

		public string UserNameCssClass
		{
			get
			{
				string state = (string) ViewState["UserNameCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["UserNameCssClass"] = value;
			}
		}

		public string UserStatisticsCssClass
		{
			get
			{
				string state = (string) ViewState["UserStatisticsCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["UserStatisticsCssClass"] = value;
			}
		}

		public string UserAreaCssClass
		{
			get
			{
				string state = (string) ViewState["UserAreaCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["UserAreaCssClass"] = value;
			}
		}

		public int MaximumNumberOfUsersToDisplay
		{
			get
			{
				object state = ViewState["MaximumNumberOfUsersToDisplay"];
				if (state == null)
					return 50;
				else
					return (int) state;
			}
			set
			{
				ViewState["MaximumNumberOfUsersToDisplay"] = value;
			}
		}

		public Unit MaximumAvatarHeight
		{
			get
			{
				object state = ViewState["MaximumAvatarHeight"];
				if (state == null)
					return Unit.Empty;
				else
					return (Unit) state;
			}
			set
			{
				ViewState["MaximumAvatarHeight"] = value;
			}
		}

		#endregion

		#region ContentScroller Implementation

		private string _contentTemplate = null;
		protected override string ContentTemplate 
		{
			get
			{
				if (_contentTemplate == null)
				{
					StringBuilder t = new StringBuilder();
					t.Append("<div class=\"");
					t.Append(this.UserAreaCssClass);
					t.Append("\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><a href=\"{3}\">{0}</a></td><td><div class=\"");
					t.Append(this.UserNameCssClass);
					t.Append("\"><a href=\"{3}\">{1}</a></div><div class=\"");
					t.Append(this.UserStatisticsCssClass);
					t.Append("\">{2}</div></td></tr></table></div>");

					_contentTemplate = t.ToString();
				}

				return _contentTemplate;
			}
		}

		private string[,] _scrollerData;
		protected override string[,] ScrollerData 
		{ 
			get
			{
				return _scrollerData;
			}
		}

		#endregion

		#region DataBind

		public override void DataBind() 
		{
			base.DataBind();
            
			IList users = UsersOnline.Members();// Users.GetUsersOnline( csContext.SiteSettings.UserOnlineTimeWindow );
			if (users != null) 
			{
				if (users.Count > this.MaximumNumberOfUsersToDisplay)
					_scrollerData = new string[this.MaximumNumberOfUsersToDisplay, 4];
				else
					_scrollerData = new string[users.Count, 4];

				int i;
				User u = null;
				for (i = 0; i < users.Count && i < this.MaximumNumberOfUsersToDisplay; i++)
				{
					u = ((UserOnline) users[i]).User;

					if (u.HasAvatar && u.EnableAvatar && u.IsAvatarApproved) 
					{
						if (!u.AvatarUrl.StartsWith("users/avatar.aspx")) 
							_scrollerData[i,0] = string.Format("<img src=\"{0}\" height=\"{1}\" border=\"0\" />", u.AvatarUrl, this.MaximumAvatarHeight.ToString());
						else 
							_scrollerData[i,0] = string.Format("<img src=\"{0}\" height=\"{1}\" border=\"0\" />", Globals.ApplicationPath + "/" + u.AvatarUrl, this.MaximumAvatarHeight.ToString());
					}
					else
						_scrollerData[i,0] = String.Empty;

					_scrollerData[i,1] = u.DisplayName;

					_scrollerData[i,2] = ResourceManager.GetString("PostFlatView_Posts") + u.TotalPosts.ToString(ResourceManager.GetString("NumberFormat"));

					if (CanSeeUserPoints())
						_scrollerData[i,2] += ", " + ResourceManager.GetString("PostFlatView_UserPoints") + u.Points.ToString(ResourceManager.GetString("NumberFormat"));

					_scrollerData[i,3] = SiteUrls.Instance().UserProfile(u.Username);
				}
			}
			else
				_scrollerData = new string[0,0];
		}

		private bool CanSeeUserPoints()
		{
			if (!csContext.SiteSettings.EnablePointSystem)
				return false;

			switch (csContext.SiteSettings.PointsUserDisplayLevel)
			{
				case DisplayLevel.None:
					return false;
				case DisplayLevel.Everyone:
					return true;
				case DisplayLevel.AdministratorsAndModerators:
					return (csContext.User.IsAdministrator || csContext.User.IsModerator);
				default:
					return false;
			}
		}
		
		#endregion		
	}
}

